/*
 * @FilePath: /vue-ts/src/permission.ts
 * @Author: Yongchao Wang
 * @Date: 2020-09-14 21:11:13
 * @LastEditors: Yongchao Wang
 * @LastEditTime: 2020-09-16 22:41:27
 */
import NProgress from 'nprogress'; // progress bar
import { NavigationGuardNext, Route } from 'vue-router';
import router from './router/index';
import store from './store';

NProgress.configure({ showSpinner: false }); // NProgress Configuration

router.beforeEach(async (to: Route, from: Route, next: NavigationGuardNext<Vue>) => {
  NProgress.start();
  const token = sessionStorage.getItem('token');
  if (to.path === '/login' || (token && token.length > 0)) {
    if (to.path === '/') {
      next({ path: '/addarticle' });
    } else {
      next();
    }
  } else {
    next({ path: '/login', query: { redirect: to.fullPath } });
  }
  NProgress.done();
});
